#!/usr/bin/python
# -*- coding: utf-8 -*-

# @File        :   db_init.py
# @Version     :   1.0
# @Author      :   zhfnss
# @Email       :   mklv2005@163.com
# @Time        :   2021/03/30 09:48:10
# Description  :   数据库初始化，第一次运行爬虫前执行 

from model.weather_info import WeatherInfo
from model.weather_city import WeatherCity
from model.weather_flag import WeatherFlag
from utils.db_util import db

from datetime import datetime


def init():
    WeatherCity.metadata.drop_all(db.engine)
    WeatherCity.metadata.create_all(db.engine)

    db.session.add_all(
        [
            WeatherCity(city_id=57687, city_name='长沙', flag=1, last_synch_date=datetime.strptime('20110101', '%Y%m%d')),
            WeatherCity(city_id=57773, city_name='湘潭', flag=1, last_synch_date=datetime.strptime('20110101', '%Y%m%d')),
            WeatherCity(city_id=57780, city_name='株洲', flag=1, last_synch_date=datetime.strptime('20110101', '%Y%m%d')),
            WeatherCity(city_id=60711, city_name='湘乡', flag=1, last_synch_date=datetime.strptime('20110101', '%Y%m%d'))
        ]
    )
    db.session.commit()
    print("初始化表weather_city完成！")

    WeatherInfo.metadata.drop_all(db.engine)
    WeatherInfo.metadata.create_all(db.engine)
    print("初始化表weather_info完成！")

    WeatherFlag.metadata.drop_all(db.engine)
    WeatherFlag.metadata.create_all(db.engine)
    print("初始化表weather_flag完成！")


if __name__ == "__main__":
    init()
